package com.cpe.web.controller.lesson;

import static com.sql.data.response.CommonRespCode.*;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpe.data.domain.lesson.Lesson;
import com.cpe.data.domain.lesson.LessonQuery;
import com.cpe.repository.mapper.lesson.LessonMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sql.data.response.JsonResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/lessons")
public class LessonController {
	@Resource
	private LessonMapper lessonMapper;

	/**
	 * 列表
	 */
	@RequestMapping(method = GET)
	public JsonResponse getPage(LessonQuery query) {
		try {
			PageHelper.startPage(query.pageWhitoutOrder());
			List<Lesson> lessons = lessonMapper.findAll();
			return JsonResponse.ok(new PageInfo<>(lessons));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return JsonResponse.custom(EXCEPTION);
		}
	}

	/**
	 * 详情
	 */
	@RequestMapping(value = "/{id}", method = GET)
	public JsonResponse getDetail(@PathVariable String id) {
		try {
			Lesson lesson = lessonMapper.findById(id);
			if (lesson != null) {
				return JsonResponse.ok(lesson);
			} else {
				return JsonResponse.custom(RESOURCE_NOT_FOUND);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return JsonResponse.custom(EXCEPTION);
		}
	}
}
